#include "form.h"
#include "ui_form.h"
#include <QtWidgets/QPushButton>
#include <QPainter>
#include <QPen>
#include <iostream>
using namespace std;

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    nwidth=20;
    nheight=40;
}

Form::~Form()
{
    delete ui;
}

void Form::paintEvent(QPaintEvent *event)
{
    QPainter p;//创建画家对象
    p.begin(this);//指定当前窗口为绘图设备，因为this是Widget也是一种绘图设备

    QPen pen;
    pen.setWidth(2);
    pen.setColor(QColor(150, 150, 150,120));

    p.setPen(pen);
    int rw=this->width()/nwidth;
    int rh=this->height()/nheight;
    for(int x=0;x<=this->width();x+=rw)
        p.drawLine(x,0,x,this->height());

    for(int y=0;y<=this->height();y+=rh)
        p.drawLine(0,y,this->width(),y);

    p.drawLine(0,this->height(),this->width(),this->height());
    p.drawLine(this->width(),0,this->width(),this->height());
    p.end();
}

void Form::init(int x, int y, int w, int h, int nw, int nh)
{
    this->setGeometry(x, y, w, h);
    this->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
    this->setAttribute(Qt::WA_TransparentForMouseEvents,true);
    setAttribute(Qt::WA_TranslucentBackground);

    if(w>=h)
        nwidth=nh,nheight=nw;
    else
        nwidth=nw,nheight=nh;

    this->update();
    this->show();

}
